﻿using System;

namespace HB.Expand
{
    public static class StringTryToNumber
    {
        //byte
        public static bool TryToByte(this string s, out byte data)
        {
            return byte.TryParse(s, out data);
        }
        public static byte TryToByte(this string s, byte failCallbackValue = 0)
        {
            return byte.TryParse(s, out byte data) ? data : failCallbackValue;
        }
        public static bool IsByte(this string s)
        {
            return byte.TryParse(s, out var data);
        }
        public static bool IsHexByte(this string s)
        {
            return s.IsRegexMatch("^[A-Fa-f0-9]{1,2}$");
        }
        //sbyte
        public static bool TryToSbyte(this string s, out sbyte data)
        {
            return sbyte.TryParse(s, out data);
        }
        public static sbyte TryToSbyte(this string s, sbyte failCallbackValue = 0)
        {
            return sbyte.TryParse(s, out sbyte data) ? data : failCallbackValue;
        }
        public static bool IsSbyte(this string s)
        {
            return sbyte.TryParse(s, out var data);
        }
        //short
        public static bool TryToShort(this string s, out short data)
        {
            return short.TryParse(s, out data);
        }
        public static short TryToShort(this string s, short failCallbackValue = 0)
        {
            return short.TryParse(s, out short data) ? data : failCallbackValue;
        }
        public static bool IsShort(this string s)
        {
            return short.TryParse(s, out var data);
        }
        //ushort
        public static bool TryToUshort(this string s, out ushort data)
        {
            return ushort.TryParse(s, out data);
        }
        public static ushort TryToUshort(this string s, ushort failCallbackValue = 0)
        {
            return ushort.TryParse(s, out ushort data) ? data : failCallbackValue;
        }
        public static bool IsUshort(this string s)
        {
            return ushort.TryParse(s, out var data);
        }
        public static bool IsHexUshort(this string s)
        {
            return s.IsRegexMatch("^[A-Fa-f0-9]{1,4}$");
        }
        //int
        public static bool TryToInt(this string s, out int data)
        {
            return int.TryParse(s, out data);
        }
        public static int TryToInt(this string s, int failCallbackValue = 0)
        {
            return int.TryParse(s, out int data) ? data : failCallbackValue;
        }
        public static bool IsInt(this string s)
        {
            return int.TryParse(s, out var data);
        }
        //uint
        public static bool TryToUint(this string s, out uint data)
        {
            return uint.TryParse(s, out data);
        }
        public static uint TryToUint(this string s, uint failCallbackValue = 0)
        {
            return uint.TryParse(s, out uint data) ? data : failCallbackValue;
        }
        public static bool IsUint(this string s)
        {
            return uint.TryParse(s, out var data);
        }
        public static bool IsHexUint(this string s)
        {
            return s.IsRegexMatch("^[A-Fa-f0-9]{1,8}$");
        }
        //long
        public static bool TryToLong(this string s, out long data)
        {
            return long.TryParse(s, out data);
        }
        public static long TryToLong(this string s, long failCallbackValue = 0)
        {
            return long.TryParse(s, out long data) ? data : failCallbackValue;
        }
        public static bool IsLong(this string s)
        {
            return long.TryParse(s, out var data);
        }
        //ulong
        public static bool TryToUlong(this string s, out ulong data)
        {
            return ulong.TryParse(s, out data);
        }
        public static ulong TryToUlong(this string s, ulong failCallbackValue = 0)
        {
            return ulong.TryParse(s, out ulong data) ? data : failCallbackValue;
        }
        public static bool IsUlong(this string s)
        {
            return ulong.TryParse(s, out var data);
        }
        public static bool IsHexUlong(this string s)
        {
            return s.IsRegexMatch("^[A-Fa-f0-9]{1,16}$");
        }
        //float
        public static bool TryToFloat(this string s, out float data)
        {
            return float.TryParse(s, out data);
        }
        public static float TryToFloat(this string s, float failCallbackValue = 0)
        {
            return float.TryParse(s, out float data) ? data : failCallbackValue;
        }
        public static bool IsFloat(this string s)
        {
            return float.TryParse(s, out var data);
        }
        //double
        public static bool TryToDouble(this string s, out double data)
        {
            return double.TryParse(s, out data);
        }
        public static double TryToDouble(this string s, double failCallbackValue = 0)
        {
            return double.TryParse(s, out double data) ? data : failCallbackValue;
        }
        public static bool IsDouble(this string s)
        {
            return double.TryParse(s, out var data);
        }
        //bool
        public static bool IsBool(this string s)
        {
            return bool.TryParse(s.ToLower(), out var data);
        }
        public static bool TryToBool(this string s, out bool data)
        {
            return bool.TryParse(s, out data);
        }
        public static bool TryToBool(this string s, bool failCallbackValue = false)
        {
            return bool.TryParse(s, out bool data) ? data : failCallbackValue;
        }
    }
}
